package com.webtest.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.StringWriter;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/8/13 10:50
 */
public class PrintUpperTag extends SimpleTagSupport {
    private String time;

    public void setTime(String time) {
        this.time = time;
    }

    @Override
    public void doTag() throws JspException, IOException {
        // 1.得到标签体内容
        JspFragment fragment = getJspBody();
        StringWriter writer = new StringWriter();
        fragment.invoke(writer);
        // 2.变为大写
        String content = writer.toString().toUpperCase();
        // 3.得到out隐含变量
        JspWriter out = getJspContext().getOut();
        // 4.循环输出
        try {
            int count = Integer.parseInt(time);
            for (int i = 0; i < count; i++) {
                out.print((i + 1) + " " + content);
                out.print("<br/>");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
